package com.jxdinfo.hussar.example.common.dict.service.impl;

import com.jxdinfo.hussar.example.common.utils.DateUtils;
import com.jxdinfo.hussar.example.common.dict.domain.SysDictSingle;
import com.jxdinfo.hussar.example.common.dict.domain.SysDictType;
import com.jxdinfo.hussar.example.common.dict.dao.SysDictSingleMapper;
import com.jxdinfo.hussar.example.common.dict.dao.SysDictTypeMapper;
import com.jxdinfo.hussar.example.common.dict.service.ISysDictTypeService;
import com.jxdinfo.hussar.example.common.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典类型Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-21
 */
@Service
public class SysDictTypeServiceImpl implements ISysDictTypeService
{
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private SysDictSingleMapper sysDictSingleMapper;
    /**
     * 项目启动时，初始化字典到缓存
     */
//    @PostConstruct
//    public void init()
//    {
//        loadingDictCache();
//    }

    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingDictCache()
    {
        SysDictSingle sysDictSingle = new SysDictSingle();
        Map<Long, List<SysDictSingle>> dictDataMap = sysDictSingleMapper.selectDictDataList(sysDictSingle).stream().collect(Collectors.groupingBy(SysDictSingle::getTypeId));
        for (Map.Entry<Long, List<SysDictSingle>> entry : dictDataMap.entrySet())
        {
            DictUtils.setDictCache(String.valueOf(entry.getKey()), entry.getValue().stream().sorted(Comparator.comparing(SysDictSingle::getSORT)).collect(Collectors.toList()));
        }
    }

    /**
     * 清空字典缓存数据
     */
    @Override
    public void clearDictCache()
    {
        DictUtils.clearDictCache();
    }

    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache() {
        clearDictCache();
        loadingDictCache();
    }

    /**
     * 查询字典类型
     *
     * @param typeId 字典类型主键
     * @return 字典类型
     */
    @Override
    public SysDictType selectSysDictTypeByTypeId(Long typeId)
    {
        return sysDictTypeMapper.selectSysDictTypeByTypeId(typeId);
    }

    /**
     * 查询字典类型列表
     *
     * @param sysDictType 字典类型
     * @return 字典类型
     */
    @Override
    public List<SysDictType> selectSysDictTypeList(SysDictType sysDictType)
    {
        return sysDictTypeMapper.selectSysDictTypeList(sysDictType);
    }

    @Override
    public List<SysDictSingle> selectDictDataByType(String dictType) {
        SysDictType sysDictType = sysDictTypeMapper.selectDictDataByType(dictType);
        return sysDictSingleMapper.selectSysDictSingleByTypeId(sysDictType.getTypeId());
    }

    /**
     * 新增字典类型
     *
     * @param sysDictType 字典类型
     * @return 结果
     */
    @Override
    public int insertSysDictType(SysDictType sysDictType)
    {
        sysDictType.setCreateTime(DateUtils.getNowDate());
        return sysDictTypeMapper.insertSysDictType(sysDictType);
    }

    /**
     * 修改字典类型
     *
     * @param sysDictType 字典类型
     * @return 结果
     */
    @Override
    public int updateSysDictType(SysDictType sysDictType)
    {
        return sysDictTypeMapper.updateSysDictType(sysDictType);
    }

    /**
     * 批量删除字典类型
     *
     * @param typeIds 需要删除的字典类型主键
     * @return 结果
     */
    @Override
    public int deleteSysDictTypeByTypeIds(Long[] typeIds)
    {
        return sysDictTypeMapper.deleteSysDictTypeByTypeIds(typeIds);
    }

    /**
     * 删除字典类型信息
     *
     * @param typeId 字典类型主键
     * @return 结果
     */
    @Override
    public int deleteSysDictTypeByTypeId(Long typeId)
    {
        return sysDictTypeMapper.deleteSysDictTypeByTypeId(typeId);
    }
    /**
     * 根据所有字典类型
     *
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeAll()
    {
        return sysDictTypeMapper.selectDictTypeAll();
    }

}
